package cn.edu.ecut.se2024.dao;
import cn.edu.ecut.se2024.model.supplier_mgmt.Supplier;
import cn.edu.ecut.se2024.model.supplier_mgmt.SupplierAddress;
import cn.edu.ecut.se2024.model.supplier_mgmt.SupplierContact;
import org.apache.ibatis.annotations.*;

import java.util.ArrayList;

@Mapper
public interface SupplierMgmtDAO {
    @Select("select * from supplier")
    ArrayList<Supplier> getAllSupplierData();

    @Select("select *from supplier_address")
    ArrayList<SupplierAddress> getAllSupplierAddressData();

    @Select("select *from supplier_contact")
    ArrayList<SupplierContact> getAllSupplierContactData();

    @Select("select * from supplier where id=#{supplier_id}")
    Supplier getSpecialSupplierData(int supplier_id);

    @Select("select * from supplier_address where sid=#{supplier_id}")
    ArrayList<SupplierAddress> getSpecialSupplierAddressData(int supplier_id);

    @Select("select * from supplier_contact where sid=#{supplier_id}")
    ArrayList<SupplierContact> getSpecialSupplierContactData(int supplier_id);

    @Insert("insert into supplier_address(sid,address,defaults) values(#{sid},#{address},#{defaults})")
    boolean addSupplierAddressData(SupplierAddress supplier_address);

    @Update("update supplier_address set address=#{address},defaults=#{defaults} where sid=#{sid} and id=#{id}")
    boolean updateSupplierAddressData(SupplierAddress supplier_address);

    @Delete("delete from supplier_address where sid=#{supplier_id} and id=#{supplier_address_id}")
    boolean deleteSupplierAddressData(int supplier_id, int supplier_address_id);

    @Insert("insert into supplier_contact(sid,name,phone,position,defaults) values(#{sid},#{name},#{phone},#{position},#{defaults})")
    boolean addSupplierContactData(SupplierContact supplier_contact);

    @Update("update supplier_contact set name=#{name},phone=#{phone},position=#{position}, defaults=#{defaults} where id=#{id} and sid=#{sid}")
    boolean updateSupplierContactData(SupplierContact supplier_contact);

    @Delete("delete from supplier_contact where sid=#{supplier_id} and id=#{supplier_contact_id}")
    boolean deleteSupplierContactData(int supplier_id, int supplier_contact_id);

    @Insert("insert into supplier(name,type,bank) values(#{name},#{type},#{bank})")
    @Options(useGeneratedKeys = true,keyProperty = "id")  // 将自增的主键返回给supplier的id属性
    boolean addSupplierData(Supplier supplier);

    @Update("update Supplier set name=#{name},type=#{type},bank=#{bank} where id=#{id}")
    boolean updateSupplierData(Supplier supplier);

    @Delete("delete from supplier where id=#{supplier_id}")
    boolean deleteSupplierData(int supplier_id);
}
